import { z } from "zod";
import { codeartsrepoRequest, buildUrl } from "../common/utils.js";
import { CommitSchema } from "../common/types.js";

// 查询某个仓库的提交信息
export const ListCommitsSchema = z.object({
  project_id: z.string().describe("项目ID"),
  repo_id: z.string().describe("仓库ID"),
  path: z.string().optional().describe("文件路径"),
  branch: z.string().optional().describe("分支名称"),
  page: z.number().optional().describe("页码，默认为1"),
  per_page: z.number().optional().describe("每页数量，默认为20"),
});

// 创建提交
export const CreateCommitSchema = z.object({
  project_id: z.string().describe("项目ID"),
  repo_id: z.string().describe("仓库ID"),
  branch: z.string().describe("分支名称"),
  commit_message: z.string().describe("提交信息"),
  actions: z.array(
    z.object({
      action: z.enum(["create", "update", "delete"]).describe("操作类型：创建、更新或删除"),
      file_path: z.string().describe("文件路径"),
      content: z.string().optional().describe("文件内容（Base64编码）"),
    })
  ).describe("文件操作列表"),
  force: z.boolean().optional().describe("是否强制提交（忽略冲突）"),
});

// 查询某个仓库的特定提交信息
export const ShowSingleCommitSchema = z.object({
  project_id: z.string().describe("项目ID"),
  repo_id: z.string().describe("仓库ID"),
  commit_id: z.string().describe("提交ID"),
});

// 查询某个仓库的提交差异信息
export const ShowDiffCommitSchema = z.object({
  project_id: z.string().describe("项目ID"),
  repo_id: z.string().describe("仓库ID"),
  commit_id: z.string().describe("提交ID"),
});

// 功能实现
export async function listCommits(
  projectId: string,
  repoId: string,
  options?: {
    path?: string;
    branch?: string;
    page?: number;
    perPage?: number;
  }
) {
  const url = buildUrl(`https://codeartsrepo.example.com/api/v2/projects/${projectId}/repositories/${repoId}/commits`, {
    path: options?.path,
    branch: options?.branch,
    page: options?.page,
    per_page: options?.perPage
  });

  const response = await codeartsrepoRequest(url);
  return z.array(CommitSchema).parse(response);
}

export async function createCommit(
  projectId: string,
  repoId: string,
  branch: string,
  commitMessage: string,
  actions: Array<{
    action: "create" | "update" | "delete";
    file_path: string;
    content?: string;
  }>,
  force?: boolean
) {
  const response = await codeartsrepoRequest(`https://codeartsrepo.example.com/api/v2/projects/${projectId}/repositories/${repoId}/commits`, {
    method: "POST",
    body: {
      branch,
      commit_message: commitMessage,
      actions,
      force
    }
  });
  
  return CommitSchema.parse(response);
}

export async function getSingleCommit(
  projectId: string,
  repoId: string,
  commitId: string
) {
  const response = await codeartsrepoRequest(`https://codeartsrepo.example.com/api/v2/projects/${projectId}/repositories/${repoId}/commits/${commitId}`);
  return CommitSchema.parse(response);
}

export async function showDiffCommit(
  projectId: string,
  repoId: string,
  commitId: string
) {
  const response = await codeartsrepoRequest(`https://codeartsrepo.example.com/api/v2/projects/${projectId}/repositories/${repoId}/commits/${commitId}/diff`);
  
  return z.array(
    z.object({
      diff: z.string(),
      new_path: z.string(),
      old_path: z.string(),
      a_mode: z.string(),
      b_mode: z.string(),
      new_file: z.boolean(),
      renamed_file: z.boolean(),
      deleted_file: z.boolean(),
    })
  ).parse(response);
} 